qx.Class.define("caraffa.forms.Nomenclador",
{
	extend : qx.ui.form.ow.FormTab,
	construct : function (usuario)
	{
		this.base(arguments, "Nomenclador", "Nomenclador de Prestaciones", "");
		
		this._perfilUsuario = caraffa.Funciones.getPerfilUsuario();
		
		var grpNomenclador = new qx.ui.groupbox.GroupBox();
		grpNomenclador.setLayout(new qx.ui.layout.VBox(10));
		
		this._cmbObrasSociales = new qx.ui.form.ow.ComboBox("Obras Sociales:", "", true);
		this._cmbPlanes = new qx.ui.form.ow.ComboBox("Planes:", "", true);

		this._btnCopiarPrestacion = new qx.ui.form.Button("Copiar Prestacion a Particular Minimo");
		this._btnCopiarPrestacion.setEnabled(false);
		
		var model = new qx.ui.table.model.Simple();
			model.setColumns(["cod_prestacion", "Prestacion", "Rubro", "Importe", "Codigo", "Fecha"]);
			model.setColumnEditable(3, true);
			model.setColumnEditable(4, true);
		
		this._grid = new qx.ui.table.Table(model);
			this._grid.getTableColumnModel().setColumnVisible(0, false);
			this._grid.setShowCellFocusIndicator(true);
//			this._grid.setColumnVisibilityButtonVisible(false);
			this._grid.setStatusBarVisible(false);
			this._grid.setColumnWidth(1, 610);
			this._grid.setColumnWidth(2, 90);
			this._grid.setColumnWidth(3, 70);
			this._grid.setColumnWidth(4, 70);
			this._grid.setColumnWidth(5, 80);
			this._grid.setWidth(950);
			this._grid.setHeight(320);
			
			var numCellRender = new qx.ui.table.cellrenderer.Conditional("right", null, null, null);
			numCellRender.addNumericCondition(">=", 0,   null, null, null, null);
			numCellRender.addNumericCondition("<=", 0,  null, null, null, null);

			this._grid.getTableColumnModel().setDataCellRenderer(3, numCellRender);

		this._btnGuardar = new qx.ui.form.Button("Guardar");
		this._btnImprimir  = new qx.ui.form.Button("Imprimir");
		if(usuario == 1 || usuario == 2  || usuario == 5) this._btnGuardar.setEnabled(true)
		else this._btnGuardar.setEnabled(false);
			
		grpNomenclador.add(this._cmbObrasSociales);
		grpNomenclador.add(this._cmbPlanes);
		grpNomenclador.add(this._btnCopiarPrestacion);
		grpNomenclador.add(this._grid);
		grpNomenclador.add(this._btnGuardar);
		grpNomenclador.add(this._btnImprimir);
		
		this.add(grpNomenclador, {left:10, top:10});
		
		var rpc = new qx.io.remote.Rpc().set({timeout:10000, url:"services/", serviceName:"nomenclador"});
		try {
			var result = rpc.callSync("getObrasSociales");
			this._cmbObrasSociales.setNewValues(result, true);
		}
		catch (ex) {
			alert(ex);
		}
		
//		var rpc = new qx.io.remote.Rpc().set({timeout:10000, url:"services/", serviceName:"nomenclador"});
//		try {
//			var result = rpc.callSync("getPrestaciones");
//			this._grid.getTableModel().setData(result);
//		}
//		catch (ex) {
//			alert(ex);
//		}
		
		this._cmbObrasSociales.addListener("changeValue", this._getPlanes, this);
		this._cmbPlanes.addListener("changeValue", this._getNomenclador, this);
		
		this._btnCopiarPrestacion.addListener("execute", function () {
//			alert(this._grid.getFocusedRow());
			if (this._grid.getFocusedRow() != null) {
				if (confirm("Esta seguro que desea copiar la prestacion para Particular Minimo?")) {
//					alert();
					var rpc = new qx.io.remote.Rpc().set({timeout:10000, url:"services/", serviceName:"nomenclador"});
					try {
						var p = {};
						p.cod_prestacion = this._grid.getTableModel().getValue(0, this._grid.getFocusedRow()); 
						var result = rpc.callSync("copiar_pm", p);
						this._getNomenclador();
					}
					catch (ex) {
						alert(ex);
					}
				}
			} else {
				alert("Debe seleccionar la Prestacion que desea copiar.");
			}
//			alert(this._grid.getTableModel().getValue(0, this._grid.getFocusedRow()));
		}, this);
		
		this._btnGuardar.addListener("execute", function ()
		{
			var leng = this._grid.getTableModel().getRowCount();
			var prestaciones = new Array();
			for (var i=0;i<leng;i++)
			{
				var prestacion = new Object();
				
//				if (!isNaN(parseFloat(this._grid.getTableModel().getValue(3,i))))
//				{
					prestacion.cod_prestacion = this._grid.getTableModel().getValue(0,i);
					prestacion.importe = this._grid.getTableModel().getValue(3,i);
					prestacion.codigo = this._grid.getTableModel().getValue(4,i);
					prestaciones.push(prestacion);
//				}
			}
			
			var params = new Object();
			params.cod_plan = this._cmbPlanes.getValue();
			params.prestaciones = prestaciones;
			
			var rpc = new qx.io.remote.Rpc().set({timeout:10000, url:"services/", serviceName:"nomenclador"});
			try {
				this._grid.cancelEditing();
				var result = rpc.callSync("setNomenclador", params);
				this._cmbObrasSociales.setValue("");
			}
			catch (ex) {
				alert(ex);
			}
			
		}, this);
		
		this._btnImprimir.addListener("execute", function () {
				window.open('reports/Nomenclador.php?obraSocial=' + this._cmbObrasSociales.getValue() + '&plan=' + this._cmbPlanes.getValuet(),'','top=0,width=1024,height=600,scrollbars=1');
		}, this);
		
	},
	members :
	{
		_getPlanes : function ()
		{
			var rpc = new qx.io.remote.Rpc().set({timeout:10000, url:"services/", serviceName:"nomenclador"});
			try {
				if (this._cmbObrasSociales.getValue() == "0")
				{
					this._btnCopiarPrestacion.setEnabled(true);
					this._cmbPlanes.setNewValues([{value:"0", label:'(Sin Plan)'}]);
				}
				else
				{
					this._btnCopiarPrestacion.setEnabled(false);
					var params = new Object();
					params.cod_obra_social = this._cmbObrasSociales.getValue();
					var result = rpc.callSync("getPlanes", params);
					this._cmbPlanes.setNewValues(result, true);
				}
			}
			catch (ex) {
				alert(ex);
			}
		},
		_getNomenclador : function ()
		{
			if ((this._cmbObrasSociales.getValue() != "") && (this._cmbPlanes.getValue() != "") && (this._cmbPlanes.getValue() != undefined))
			{
				var rpc = new qx.io.remote.Rpc().set({timeout:10000, url:"services/", serviceName:"nomenclador"});
				try {
					var params = new Object();
					params.cod_plan = this._cmbPlanes.getValue();
					var result = rpc.callSync("getPrestaciones", params);
					this._grid.getTableModel().setData(result);
				}
				catch (ex) {
					alert(ex);
				}
			}
			else
			{
				this._grid.getTableModel().removeRows(0,(this._grid.getTableModel().getRowCount()));
			}
		},
		_setValues : function (values)
		{
			//alert(qx.util.Json.stringify(values, true));
			alert(qx.lang.Json.stringify(values, "", " "));
		}
		
	}	
});